package com.blacktry.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.pojo.Book;
import com.blacktry.pojo.Lend;
import com.blacktry.pojo.RespBean;
import com.blacktry.service.BookService;
import com.blacktry.service.LendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author blackTry
 * @since 2021-01-31
 */
@RestController
@RequestMapping("/lend")
public class LendController {
    @Autowired
    private LendService lendService;

    @Autowired
    private BookService bookService;

    @PostMapping("/save")
    public RespBean save(@RequestBody Lend lend){
        if(lendService.save(lend)){
            return RespBean.success("添加成功！");
        }else{
            return RespBean.error("添加失败！");
        }
    }

    @DeleteMapping("/delete/{id}")
    public RespBean delete(@PathVariable("id") Integer id){
        if(lendService.removeById(id)){
            return RespBean.success("删除成功！");
        }else{
            return RespBean.error("删除失败！");
        }
    }

    @PutMapping("/update")
    public RespBean update(@RequestBody Lend lend){
        if(lendService.updateById(lend)){
            return RespBean.success("添加成功！");
        }else{
            return RespBean.error("添加失败！");
        }
    }

    @GetMapping("/findAll/{currentPage}/{size}")
    public Page<Lend> findAll(@PathVariable("currentPage")Integer currentPage, @PathVariable("size")Integer size){
        return lendService.selectPage(currentPage,size);
    }

    // 正在借阅
    @GetMapping("/findNow/{currentPage}/{size}")
    public Page<Lend> findNow(@PathVariable("currentPage")Integer currentPage, @PathVariable("size")Integer size){
        return lendService.selectNowPage(currentPage,size);
    }

    // 已完成借阅历史查询
    @GetMapping("/findSuccess/{currentPage}/{size}")
    public Page<Lend> findSuccess(@PathVariable("currentPage")Integer currentPage, @PathVariable("size")Integer size){
        return lendService.selectSuccessPage(currentPage,size);
    }

    @GetMapping("findById/{id}")
    public Lend findById(@PathVariable("id")Integer id){
        return lendService.getById(id);
    }
}

